package old;

import edu.cmu.abi.Artist;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class SimilarArtists {
    private static String artist_name;


    public String getArtistName() {
          return artist_name;
    }

    public void setName(String s) {
          artist_name = s;
    }

    public static ResultSet readFromDatabase(String name, Connection con) throws SQLException {
         PreparedStatement statement = null;

         try {

             statement = con.prepareStatement("select track_id "+
                                              "from songs where UPPER(artist_name) = UPPER(?)");

             statement.setString(1,name);

             ResultSet result = statement.executeQuery();

             if(result.next()) {
                 artist_name = name;
             }

             else {
                 System.out.println("Could not read the artist name, please enter a correct artist name");;
             }
             return result;
         }
         
         catch (Exception e){
             System.out.println("Problem with read");
             return null;
         }
    }

    public static Artist OriginalArtist(ResultSet R){
        Artist A = new Artist();
        A.setName(artist_name);
        ArrayList<String> songs = new ArrayList<String>();

        ResultSetMetaData rsm = null;
        try {
            rsm = R.getMetaData();

            while (R.next()){
                for(int col = 1; col <= rsm.getColumnCount(); col++){
                    songs.add(R.getString(col));
                }
            }


        } catch (SQLException ex) {
            System.out.println("Wasn't able to create the original artist entered by user");
        }
        //A.setListOfSongs(songs);

        return A;
    }

    public static HashMap createArtists(ArrayList<String[]> AL){
        String[] S = new String[4];
        HashMap hm = new HashMap();
        for (int i = 0; i<AL.size(); i++){
            S = AL.get(i);
            hm.put(S[0], S[1]);
        }
        return hm;

    }


    public static ResultSet read(String name, Connection con) throws SQLException {

         PreparedStatement statement = null;

         try {

             statement = con.prepareStatement("select artist_id, track_id, artist_name, title "+
                                                "from songs where artist_id in " +
                                                "(select distinct similar from similarity " +
                                                "where target = (select distinct artist_id " +
                                                "from songs where UPPER(artist_name)= UPPER(?) " +
                                                "and rownum<2)) order by artist_id");

             statement.setString(1,name);

             ResultSet result = statement.executeQuery();

             if(result.next()) {
                 artist_name = name;
             }
             
             else {
                 System.out.println("Could not read the artist name, please enter a correct artist name");
             }
             return result;
         }
         catch (Exception e){
             System.out.println("Problem with read");
             return null;
         }
    }

    public static ResultSet betterRead(String name, Connection con) throws SQLException {

         PreparedStatement statement = null;

         try {

             statement = con.prepareStatement("select artist_id, track_id, artist_name, title "+
                                                "from songs where artist_id in "+
                                                "(select distinct similar from similarity "+
                                                "where target = (select distinct artist_id "+
                                                "from songs where UPPER(artist_name)= UPPER(?) "+
                                                "and rownum<2) union select distinct target "+
                                                "from similarity where similar = " +
                                                "(select distinct artist_id from songs "+
                                                "where UPPER(artist_name)= UPPER(?) and rownum<2)) "+
                                                "order by artist_id");

             statement.setString(1,name);
             statement.setString(2,name);

             ResultSet result = statement.executeQuery();

             if(result.next()) {
                 artist_name = name;
             }

             else {
                 System.out.println("Could not read the artist name, please enter a correct artist name");;
             }
             return result;
         }
         catch (Exception e){
             System.out.println("Problem with read");
             return null;
         }
    }


   public static void main(String args[]) throws SQLException, ClassNotFoundException, Exception  {

        Connection connection = null;
        ArrayList<String[]> aList = new ArrayList<String[]>();

        try {
            String driverName = "oracle.jdbc.driver.OracleDriver";
            Class.forName(driverName);

            String serverName = "localhost";
            String portNumber = "1521";
            String sid = "XE";
            String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
            String username = "system";
            String password = "johnheinz";
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("Built connection");
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
               SimilarArtists SA = new SimilarArtists();

               ResultSetMetaData rsm = null;

               String answer = "";

               BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
               System.out.println("Please enter the artist name:");
               String artistname = stdIn.readLine();

               ResultSet rs = SA.read(artistname, connection);
               Artist EnteredArtist = OriginalArtist(readFromDatabase(artistname, connection));

               rsm = rs.getMetaData();


               try {
                    int counter = 0;
                    
                    while(rs.next()) {
                        int counter2=0;
                        String[] s = new String[4];
                      for(int col = 1; col <= rsm.getColumnCount(); col++){
                           s[counter2]=rs.getString(col);
                           counter2 +=1;
                           counter+=1;

                           answer += rs.getString(col) + "  ";

                           if (counter %4 == 0){
                               answer +="\n";
                               aList.add(s);

                           }
                       }
                   }
                   System.out.println("There are "+counter/4+" results");
                   HashMap SimilarSongsByArtists = createArtists(aList);
                   
                   Object[] result = {EnteredArtist, SimilarSongsByArtists};
                   System.out.println("The size of the hashmap: " +SimilarSongsByArtists.size());
                   connection.close();
               }
               
               catch (SQLException sqle) {
                  System.err.println("Exception caught in main:" + sqle);
               }

               System.out.println(answer);
               System.out.println("Size of the arraylist: "+aList.size());
               connection.close();
   }
}